﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Collections;
using System.Text.RegularExpressions;
using System.Threading;
namespace FTchina.RSS
{
    /// <summary>
    ///getrss 的摘要说明
    /// </summary>
    public class getrss
    {
        public getrss()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 获取所有的RSS
        /// </summary>
        /// 
        [STAThread]
        public static void GetAllRSS()
        {

          

           DataTable dt = shujuku.GetDataTable("select * from rss order by id desc","test");
           for(int i=0;i<dt.Rows.Count;i++)
           {
                GetRSS(dt.Rows[i]["rssurl"].ToString(), dt.Rows[i]["id"].ToString());
           }

        }




        public static void GetRSS(string url, string rssid)
        {
            try
            {

                string guizeid = shujuku.GetValueByKey("guizeid,id", "rss", "id=" + rssid, "rssguize" + rssid);
                DataTable dturlreplace = shujuku.GetDataTable("select * from rssguize where id=" + guizeid);
                string urlold = dturlreplace.Rows[0]["urlold"].ToString();
                string urlnew = dturlreplace.Rows[0]["urlnew"].ToString();


                XmlDocument rss = new XmlDocument();
                rss.Load(url);
                XmlNamespaceManager namespacemanager = new XmlNamespaceManager(rss.NameTable);
                namespacemanager.AddNamespace("myrss", url);
                XmlNodeList list = rss.SelectNodes("/rss/channel/item", namespacemanager);


                string title = string.Empty;
                string artlink = string.Empty;
                string artjianjie = string.Empty;
                string artsj = string.Empty;
                if (list != null)
                {
                    ArrayList sqls = new ArrayList();
                    foreach (XmlNode node in list)
                    {
                        XmlNode Title = node.SelectSingleNode("title");
                        if (Title != null) title = basic.SafeRequest(NoSpecialChar(NoHTML(Title.InnerText)));
                        XmlNode Link = node.SelectSingleNode("link");
                        if (Link != null) artlink = Link.InnerText;
                        if (urlold.Length > 0)
                        {
                            artlink = artlink.Replace(urlold, urlnew);
                        }
                        XmlNode Jianjie = node.SelectSingleNode("description");
                        if (Jianjie != null) artjianjie =basic.GetSubString(basic.SafeRequest(NoSpecialChar(NoHTML(Jianjie.InnerText))),300,"……");
                        XmlNode Date = node.SelectSingleNode("description");
                        if (Date != null) artsj = NoSpecialChar(NoHTML(Date.InnerText));

                        try
                        {
                            artsj = Convert.ToDateTime(artsj).ToString();
                        }
                        catch
                        {
                            artsj = DateTime.Now.ToString();
                        }
                        sqls.Add("insert into rssurllist (rssid,title,url,jianjie,sj) values ('" + rssid + "','" + title + "','" + artlink + "','" + artjianjie + "','" + artsj + "')");
                    }


                    shujuku.ExecuteSqlTransaction(sqls);

                }



            }
            catch { }

        }

        public static string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
              RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "",
              RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
          

            return Htmlstring;
        }


        public static string NoSpecialChar(string strinput)
        {
            string result = "";
            result = strinput.Replace("\"", "\\\"");
            result = result.Replace("\\", "\\\\");
            result = result.Replace("/", "\\/");
            result = result.Replace("\b", "\\b");
            result = result.Replace("\f", "\\f");
            result = result.Replace("\n", "\\n");
            result = result.Replace("\r", "\\r");
            result = result.Replace("\t", "\\t");
            return result;
        }


    }
}